package com.domor.xxx.framework.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static springfox.documentation.builders.RequestHandlerSelectors.basePackage;

/**
 * @Author: liyuyang
 * @Date: 2021/12/13 10:44
 * Swagger配置
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@Profile({"dev", "test"})
public class SwaggerConfiguration {

    @Bean
    public Docket groupRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(getApiInfo())
                .groupName("1.0版本")
                .useDefaultResponseMessages(false)
                .select()
                .apis(basePackage("com.domor.xxx.app.controller"))
                .paths(PathSelectors.any())
                .build().securityContexts(getSecurityContexts()).securitySchemes(getSecuritySchemes());
    }

    /**
     * API详情配置
     */
    private ApiInfo getApiInfo() {
        return new ApiInfoBuilder()
                .title("动脉智能接口文档")
                .description("<div style='font-size:14px;color:red;'>Domor swagger RESTful APIs</div>")
                .termsOfServiceUrl("http://127.0.0.1:8080/")
                .version("1.0")
                .build();
    }

    /**
     * 安全上下文。即存储认证授权的相关信息，实际上就是存储"当前用户"账号信息和相关权限
     */
    private List<SecurityContext> getSecurityContexts() {
        // 此处为list，即可以设置多个
        return Lists.newArrayList(securityContext());
    }

    /**
     * 安全上下文。即存储认证授权的相关信息，实际上就是存储"当前用户"账号信息和相关权限
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("/.*"))
                .build();
    }

    /**
     * 安全引用
     */
    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Lists.newArrayList(new SecurityReference("BearerToken", authorizationScopes));
    }

    /**
     * 描述API如何保护（基本认证，OAuth2，...）。
     */
    private ArrayList<SecurityScheme> getSecuritySchemes() {
        ApiKey apiKey = new ApiKey("BearerToken", "Authorization", "header");
        return Lists.<SecurityScheme>newArrayList(apiKey);
    }

}